package com.link.base.base.servicerequest.service;

import com.link.base.base.servicerequest.dao.mybatis.mapper.ServiceAcceptMapper;
import com.link.base.base.servicerequest.dao.mybatis.mapper.ServiceRequestMapper;
import com.link.base.base.servicerequest.model.ServiceRequest;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.LovUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @author unknown
 * @date unknown
 * @comments 服务接收请求
 */
@Service
public class ServiceAcceptServiceImpl extends BasicServiceImpl<ServiceRequest> implements ServiceAcceptService {

    @Resource
    private ServiceAcceptMapper serviceAcceptMapper;
    @Resource
    private ServiceRequestMapper serviceRequestMapper;

    @Override
    public BasicMapper<ServiceRequest> getBasicMapper() {
        return serviceAcceptMapper;
    }


    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void submit(ServiceRequest entity) throws Exception {
        ServiceRequest validateObj = serviceRequestMapper.queryById(entity);
        if (validateObj == null) {
            throw new ServiceException("SERVICEREQUEST-003");
        }
        //获取独立源代码，用于逻辑判断
        String value = LovUtil.getLovValue("SERVICE_ACCEPT_STATUS", validateObj.getStatus());
        if (StringUtils.isNotBlank(value)) {
            //设置独立源代码，并代替了显示值
            validateObj.setStatus(value);
        }
        String newStr = "New";
        if (!newStr.equals(validateObj.getStatus())) {
            throw new ServiceException("SERVICEREQUEST-004",validateObj.getStatus());
        }
        validateObj.setStatus("Submitted");
        statusUpdate(validateObj);
        //flowService.createFlow(validateObj.getId(), "Test Hqq", "编号:"+validateObj.getServiceCode());
    }

    @Override
    public void statusUpdate(ServiceRequest entity) throws Exception {
        if (null == entity.getId()) {
            throw new ServiceException("PUBLIC-007");
        }
        if (StringUtils.isNull(entity.getStatus())) {
            throw new ServiceException("SERVICEREQUEST-005");
        }
        serviceAcceptMapper.statusUpdate(entity);
    }

}
